﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
 

namespace TracNghiemTinHocDaiCuong.Common
{
    public sealed class SqlHelper
    {
        public static IDataReader ExecuteReader(string query,
            Collection<SqlParameter> parameter)
        {
            IDataReader result = null;
            try
            {
                var connect = new SqlConnection(AppConfig.connectionString);
                connect.Open();
                var cmd = new SqlCommand(query, connect)
                {
                    CommandType = CommandType.Text
                };
                cmd.Parameters.Clear();

                if (parameter != null)
                {
                    for (int i = 0; i < parameter.Count; i++)
                    {
                        cmd.Parameters.Add(parameter[i]);
                    }
                }
                result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (parameter != null)
                {
                    for (int i = 0; i < parameter.Count; i++)
                    {
                        if (parameter[i].Direction == ParameterDirection.Output ||
                            parameter[i].Direction == ParameterDirection.InputOutput ||
                            parameter[i].Direction == ParameterDirection.ReturnValue)
                        {
                            parameter[i].Value = cmd.Parameters[parameter[i].ParameterName].Value;
                        }
                    }
                }
                return result;
            }
            catch (System.Exception ex)
            {
                if (result != null)
                    result.Close();
                return null;
            }
        }

        public static IDataReader ExecuteReader(string query)
        {
            IDataReader result = null;
            try
            {
                var connect = new SqlConnection(AppConfig.connectionString);
                connect.Open();
                var cmd = new SqlCommand(query, connect)
                {
                    CommandType = CommandType.Text
                };
                result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return result;
            }
            catch (System.Exception ex)
            {
                if (result != null)
                    result.Close();
            }
            return result;
        }

        public static IDataReader QueryString(string query)
        {
            IDataReader result = null;
            try
            {
                var connect = new SqlConnection(AppConfig.connectionString);
                connect.Open();
                var cmd = new SqlCommand(query, connect)
                {
                    CommandType = CommandType.Text
                };
                result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return result;
            }
            catch (SqlException)
            {
                if (result != null)
                    result.Close();
                throw;
            }
        }

        public static DataTable ExecuteQuery(string query)
        {
            try
            {
                var result = new DataTable { Locale = CultureInfo.CurrentUICulture};
                var connect = new SqlConnection(AppConfig.connectionString);
                connect.Open();
                var cmd = new SqlCommand(query, connect)
                {
                    CommandType = CommandType.Text
                };
                cmd.Parameters.Clear();
                var sqlAdapter = new SqlDataAdapter(cmd);
                sqlAdapter.Fill(result);
                connect.Close();
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        public static DataTable ExecuteQuery(string query,
            Collection<SqlParameter> parameter)
        {
            try
            {
                var result = new DataTable { Locale = CultureInfo.CurrentUICulture };
                var connect = new SqlConnection(AppConfig.connectionString);
                connect.Open();
                var cmd = new SqlCommand(query, connect)
                {
                    CommandType = CommandType.Text
                };
                cmd.Parameters.Clear();
                if (parameter != null)
                {
                    for (int i = 0; i < parameter.Count; i++)
                    {
                        cmd.Parameters.Add(parameter[i]);
                    }
                }
                var sqlAdapter = new SqlDataAdapter(cmd);
                sqlAdapter.Fill(result);
                connect.Close();
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        public static int ExecuteNonQuery(string query,
            Collection<SqlParameter> parameter)
        {
            var connect = new SqlConnection(AppConfig.connectionString);
            connect.Open();
            var cmd = new SqlCommand(query, connect)
            {
                CommandType = CommandType.Text
            };
            cmd.Parameters.Clear();
            if (parameter != null)
            {
                for (int i = 0; i < parameter.Count; i++)
                {
                    cmd.Parameters.Add(parameter[i]);
                }
            }
            int result = cmd.ExecuteNonQuery();
            if (parameter != null)
            {
                for (int i = 0; i < parameter.Count; i++)
                {
                    if (parameter[i].Direction == ParameterDirection.Output ||
                        parameter[i].Direction == ParameterDirection.InputOutput ||
                        parameter[i].Direction == ParameterDirection.ReturnValue)
                    {
                        parameter[i].Value = cmd.Parameters[parameter[i].ParameterName].Value;
                    }
                }
            }
            connect.Dispose();
            return result;
        }

        public static int ExecuteNonQuery(SqlConnection connect,
            SqlTransaction transact, string query, 
            Collection<SqlParameter> parameter)
        {
            var cmd = new SqlCommand(query, connect, transact)
            {
                CommandType = CommandType.Text
            };
            cmd.Parameters.Clear();
            if (parameter != null)
            {
                for (int i = 0; i < parameter.Count; i++)
                {
                    cmd.Parameters.Add(parameter[i]);
                }
            }
            int result = cmd.ExecuteNonQuery();
            if (parameter != null)
            {
                for (int i = 0; i < parameter.Count; i++)
                {
                    if (parameter[i].Direction == ParameterDirection.Output ||
                        parameter[i].Direction == ParameterDirection.InputOutput ||
                        parameter[i].Direction == ParameterDirection.ReturnValue)
                        parameter[i].Value = cmd.Parameters[parameter[i].ParameterName].Value;
                }
            }
            return result;
        }

        public static int ExecuteNonQuery(string query)
        {
            var connect = new SqlConnection(AppConfig.connectionString);
            connect.Open();
            var cmd = new SqlCommand(query, connect)
            {
                CommandType = CommandType.Text
            };
            cmd.Parameters.Clear();
            int result = cmd.ExecuteNonQuery();
            connect.Dispose();
            return result;
        }

        public static int ExecuteNonQuery(SqlConnection connect,
            SqlTransaction transact, string query)
        {
            var cmd = new SqlCommand(query, connect, transact)
            {
                CommandType = CommandType.Text
            };
            cmd.Parameters.Clear();
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public static DataTable ExcecuteDataAdapter(string query)
        {
            var table = new DataSet();
            table.Locale = CultureInfo.InvariantCulture;
            var connect = new SqlConnection(AppConfig.connectionString);
            var adapter = new SqlDataAdapter(query, connect);
            adapter.SelectCommand.CommandType = CommandType.Text;
            adapter.Fill(table);
            return table.Tables[0];
        }

        public static DataTable ExcecuteDataAdapter(string query,
            Collection<SqlParameter> parameter)
        {
            var table = new DataSet();
            table.Locale = CultureInfo.InvariantCulture;
            var connect = new SqlConnection(AppConfig.connectionString);
            var adapter = new SqlDataAdapter(query, connect);
            adapter.SelectCommand.CommandType = CommandType.Text;
            if (parameter != null)
            {
                for (int i = 0; i < parameter.Count; i++)
                {
                    adapter.SelectCommand.Parameters.Add(parameter[i]);
                }
            }
            adapter.Fill(table);
            if (parameter != null)
            {
                for (int i = 0; i < parameter.Count; i++)
                {
                    if (parameter[i].Direction == ParameterDirection.Output ||
                        parameter[i].Direction == ParameterDirection.InputOutput ||
                        parameter[i].Direction == ParameterDirection.ReturnValue)
                        parameter[i].Value = adapter.SelectCommand.Parameters[parameter[i].ParameterName].Value;
                }
            }
            return table.Tables[0];
        }
    }
}